<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 创建对象
        // 工厂模式
        function Factory(name, age) {
            var obj = {};
            obj.name = name;
            obj.age = age;
            return obj;
        }
        // 自定义构造函数
        function Person(name, age) {
            this.name = name;
            this.age = age;
            return {name: '周杰伦'}
        }
        /**
         * new 关键字的作用
         * 1. 创建一个空对象
         * 2. this指向这个空对象
         * 3. 实例的隐式原型指向函数的原型对象
         * 4. 如果该函数没有返回对象，隐式返还这个对象,返回this
          */
        //  模拟
        function mynew(name, age) {
            var obj = {};
            var res = Person.call(obj, name, age);
            obj.__proto__ = Person.prototype;
            if (res === "undefined") {
                return obj;
            } else {
                return res;
            }
            
        }
        var obj1 = mynew('王俊凯',  18);
        console.log(obj1);
    </script>
</body>
</html>